
//use "innvlevekaar2016.dta", clear



// Covariates
recode Kjonn (2=0),g(male)
tab landbak3gen, gen(cntry_)
recode D4_ny (1=1)(else=0),g(emp_ancestry)
recode D13 (5 6 = 1 )(1 2 3 4= 0)(else=0),g(father_edu)
recode D15_ny (1=1)(else=0),g(father_emp)
recode E1 (3 = 1)(else=0),g(islam)
recode D5 (1=1)(else=0),g(fam_arrival)

// Outcomes
recode Arbeid1a (2=0)(8 9 = .),g(emp)
recode B18d (98/99=.),g(democracy)
recode PW130 (98/99=.),g(trust)
recode  D7_B (8 9 = .),g(ancestry_attachment)
recode  D7_ny (8 9 = .),g(norway_attachment)
recode D6_ny (1=1)(2 3 4 = 0)(else=.), g(stay_in_norway)

replace Q8c = . if Q8c > 5
g freedom_speech = Q8c

g discrim = 0
replace discrim = 1 if Diskr1_2==1 
replace discrim = 1 if discrim == 0 & Diskr2_2==1
replace discrim = 1 if discrim == 0 & Diskr3_3==1
replace discrim = 1 if discrim == 0 & Diskr4_3==1
replace discrim = 1 if discrim == 0 & Diskr5==1

replace Spm48a = . if Spm48a > 4
replace Spm48b = . if Spm48b > 4
replace Spm48c = . if Spm48c > 4
replace Spm48d = . if Spm48d > 4
recode Spm48b (1=4)(2=3)(3=2)(4=1), gen(Spm48b_recode)
g gender_role_index = (Spm48a  + Spm48b_recode + Spm48c + Spm48d)/4

recode Q4_ny (1 2 3=0)(4 5=1)(else=.),g(imfriends)

replace yrkinnt_1 = 0 if yrkinnt_1<0



// Create cohorts and groups
g cohort = 0
replace cohort = 1 if botid_gruppert==4  
replace cohort = 1 if botid_gruppert==3  
replace cohort = 1 if botid_gruppert==2  
replace cohort = . if botid_gruppert==6
replace cohort = . if botid_gruppert==1

g group = 0 
replace group = 1 if innvgrunn=="FLU"
replace group = 1 if innvgrunn==" " & D1_ny == 4
replace group = . if intro==1 & innvgrunn!="FLU"
replace group = . if innvgrunn==" " & D1_ny == 3
replace group = . if botid_gruppert==6
replace group = . if botid_gruppert==1

g cohortXgroup = cohort*group

g sample = 0
replace sample = 1 if  botid_gruppert==4|botid_gruppert==5


// Standardize outcomes
foreach x in emp  yrkinnt_1 norway_attachment ancestry_attachment trust democracy discrim imfriends gender_role_index stay_in_norway freedom_speech {
	egen std_`x' = std(`x') if cohort!=.
}

global controls i.Aldgrupp male father_edu father_emp emp_ancestry islam fam_arrival cntry_*

// Relabel

label variable cohortXgroup "Post*Ref"
label variable cohort  "Post"
label variable group "Refugee"
label variable std_emp "Employment"
label variable std_yrkinnt_1 "Earnings (10K NOK)" 
label variable std_norway_attachment "Attachment Norway"
label variable std_ancestry_attachment "Attachment ancestry" 
label variable std_trust "Political trust"
label variable std_democracy "Democracy important" 
label variable std_discrim "Experienced discrimination"
label variable std_imfriends "Ethnic Norwegian friends"
label variable std_stay_in_norway "Stay in Norway"
label variable std_gender_role_index "Support Traditional Gender Roles"
label variable std_freedom_speech "Support Freedom of Speech"


foreach y in std_emp  std_yrkinnt_1 std_norway_attachment std_ancestry_attachment std_trust std_democracy std_discrim std_imfriends std_gender_role_index std_stay_in_norway std_freedom_speech {
 	reg `y' cohortXgroup cohort group $controls [pw=vekter]
 	est store `y'
 	estadd local controls Yes
 	estadd ysumm, replace
 }

#delimit;
esttab std_emp  std_yrkinnt_1 std_norway_attachment std_ancestry_attachment std_trust std_democracy std_discrim std_imfriends std_gender_role_index std_stay_in_norway std_freedom_speech  using "tablea22.tex",
b(%12.3f) style(tex) 
	stats(controls N ymean, fmt(0 2) labels ("Controls" "N" "Mean of Y"))
	se nomtitles nonotes nonumbers
	keep(cohortXgroup cohort group)
    varlabels (cohortXgroup "Post Rollout*Refugee" cohort "Post Rollout" group "Refugee") 
	prehead("\begin{tabular}{l*{@M}{c}}" "\hline") 
	postfoot("\hline" "\end{tabular}")
	replace;
 
#delimit cr

keep $controls std_emp  std_yrkinnt_1 std_norway_attachment std_ancestry_attachment std_trust std_democracy std_discrim std_imfriends std_gender_role_index std_stay_in_norway std_freedom_speech 
// Save dataset for R graphing
save innvlevekaar2016_t.dta, replace



